Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HPCC-32795 Additional optimizations to replaceString #19239

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

jackdelv
Copy link
Contributor

@jackdelv jackdelv commented Oct 25, 2024

  • Does nothing if no search string is supplied or if it is larger than the source string
  • Compare directly if search and source string are equal in length

Type of change:

  • This change is a bug fix (non-breaking change which fixes an issue).
  • This change is a new feature (non-breaking change which adds functionality).
  • This change improves the code (refactor or other change that does not change the functionality)
  • This change fixes warnings (the fix does not alter the functionality or the generated code)
  • This change is a breaking change (fix or feature that will cause existing behavior to change).
  • This change alters the query API (existing queries will have to be recompiled)

Checklist:

  • My code follows the code style of this project.
    • My code does not create any new warnings from compiler, build system, or lint.
  • The commit message is properly formatted and free of typos.
    • The commit message title makes sense in a changelog, by itself.
    • The commit is signed.
  • My change requires a change to the documentation.
    • I have updated the documentation accordingly, or...
    • I have created a JIRA ticket to update the documentation.
    • Any new interfaces or exported functions are appropriately commented.
  • I have read the CONTRIBUTORS document.
  • The change has been fully tested:
    • I have added tests to cover my changes.
    • All new and existing tests passed.
    • I have checked that this change does not introduce memory leaks.
    • I have used Valgrind or similar tools to check for potential issues.
  • I have given due consideration to all of the following potential concerns:
    • Scalability
    • Performance
    • Security
    • Thread-safety
    • Cloud-compatibility
    • Premature optimization
    • Existing deployed queries will not be broken
    • This change fixes the problem, not just the symptom
    • The target branch of this pull request is appropriate for such a change.
  • There are no similar instances of the same problem that should be addressed
    • I have addressed them here
    • I have raised JIRA issues to address them separately
  • This is a user interface / front-end modification
    • I have tested my changes in multiple modern browsers
    • The component(s) render as expected

Smoketest:

  • Send notifications about my Pull Request position in Smoketest queue.
  • Test my draft Pull Request.

Testing:

- Does nothing if no search string is supplied or if it is larger than the source string
- Compare directly if search and source string are equal in length

Signed-off-by: Jack Del Vecchio <[email protected]>
Copy link

Jira Issue: https://hpccsystems.atlassian.net//browse/HPCC-32795

Jirabot Action Result:
Workflow Transition To: Merge Pending
Updated PR

Copy link
Contributor

@dcamper dcamper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs a slight change.

if (oldlen == curLen)
{
if (memcmp(buffer, oldStr, oldlen) == 0)
temp.append(newlen, newStr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would think this would be more efficient to copy newStr directly into buffer, rather than going through temp.

If you make that change, then temp and the call to swap() can be moved to the same block.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it would be more efficient to copy directly and only use the temp buffer when necessary.

- Add unit tests for special case
@jackdelv jackdelv requested a review from dcamper October 25, 2024 14:54
Copy link
Contributor

@dcamper dcamper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

Copy link
Member

@ghalliday ghalliday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR only partially implements the suggestions in the jira ticket.

  1. No replacements take place
    This covers the case where the text is smaller than the search text, but does not cover the more general case where there were matches.
    Hint: What parameter could be passed, and how could a common function efficiently interpret it to avoid a memcpy and memory allocation if no search string was found?
  1. The source and target replacement strings are the same length
    You have implement the source and current string are the same length.
    Hint: How can you optimize and in-place replace if the source and target strings are the same length?

There is actually a 3rd case:

  1. The replacement text is shorter than the search text
    Question: What optimization does this enable for an in-place replacement? Can you efficiently common this up with case (2) to avoid duplicating too much code.

{
if (memcmp(buffer, oldStr, oldlen) == 0)
{
memcpy(buffer, newStr, newlen);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I was reviewing this code (I am not because of general review comments) - this has a serious memory corruption if newlen > oldlen

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. I added ensureCapacity(newlen).

- Add ensureCapacity(newlen)
- Add additional parameter to replaceString that returns whether a match was found
- Wait to allocate memory until a match is made rather than at the beginning of the search
- Avoid copying source into result if source wasn't changed
- Move the target into the source rather than copy and skip allocating.
- Add tests for each case of target length
while (offset < maxOffset)
{
if (unlikely(source[offset] == firstChar)
&& unlikely((lenOldStr == 1) || memcmp(source + offset, oldStr, lenOldStr)==0))
{
// Wait to allocate memory until a match is found
if (unlikely(!foundMatch))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ghalliday I am not sure I got this correct. I chose unlikely because it will only ever be true on the first match, so any string with multiple matches will get some benefit. If this is rare and in most cases there is only a single match then it should probably be likely. Is that correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the time I wouldn't worry about adding likely/unlikely. The case where it is may be important is in inner loops that are the critical points. Once we have a match it is much less critical.

@jackdelv
Copy link
Contributor Author

jackdelv commented Oct 29, 2024

@ghalliday I believe I got all the optimizations you were asking for.

  1. I changed replaceString to wait to call ensureCapacity until a match has been found. This way if no matches are found the toplevel replaceString never allocated any extra memory and returns itself instead of swapping with temp.
  2. If the source string is the same size as the target, the memory can be moved rather than copy because we know there is space. This was the one I was least sure about. Is there a better way to move the memory?
  3. If the target is smaller then the first step we can do the same thing as optimization 2, but we have to adjust the curLen.

Let me know if I got any of these wrong. Back to you.

Copy link
Member

@ghalliday ghalliday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this implements the logic to not copy if it hasn't been modified - although a couple of suggestions to clean up.
It doesn't tackle the second suggestion of replacement text <= match text

::replaceString(result, rtlUtf8Size(scriptChars, script), script, rtlUtf8Size(searchChars, search), search, rtlUtf8Size(outFieldsChars, outFields), outFields);
bool foundMatch = false;
size_t sourceLen = rtlUtf8Size(scriptChars, script);
::replaceString(result, sourceLen, script, rtlUtf8Size(searchChars, search), search, rtlUtf8Size(outFieldsChars, outFields), outFields, foundMatch);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is the only place replaceString is called then I would consider a slightly different approach.
i) Pass a flag to indicate whether to copy if there is no match
ii) change the return type to be a boolean and return whether or not a replace occurred.

otherwise it is a slightly strange semantics for a pubic function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to return a bool indicating if a match was found and takes a parameter that sets whether to copy regardless of a match being found.

while (offset < maxOffset)
{
if (unlikely(source[offset] == firstChar)
&& unlikely((lenOldStr == 1) || memcmp(source + offset, oldStr, lenOldStr)==0))
{
// Wait to allocate memory until a match is found
if (unlikely(!foundMatch))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the time I wouldn't worry about adding likely/unlikely. The case where it is may be important is in inner loops that are the critical points. Once we have a match it is much less critical.

- Returns a bool indicating whether a match was made
- Accepts a parameter to force a copy even if no match was found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants